// @charset "utf-8";

define(function(require, exports, module) {
	var publicMethod = require('../../public/public'),
		clientApi = require('../../public/clientapi');

	var IndexBase  = require('../allindex/indexbase');

	function ZqxyModule () {
		this.domId = 'zqxy';
		this.MODULENAME = '赚钱效应';
		this.EXPLAINTEXT = '132465实时提示两市个股涨跌停的数量。涨跌停数量反应了短期市场的强弱，涨停个股越多，市场越强；反之需警惕市场走弱。';

		this.type = 'min';

		this.code = '883900,1A0001';
		this.indexCode = '883900';
		this.hcType = 'zqxypjhc';

		this.latestVal1 = '--';
		this.latestVal2 = '--';

		this.HighchartsObj = null;
		this.renderDom();
	}

	publicMethod.extendClass(ZqxyModule, IndexBase);

	ZqxyModule.prototype.renderDom = function () {
		var self = this;
		var htmlDom = '<li data-mname="'+ self.domId +'">' +
					'<div class="module-item">' +
						'<div class="module-header">' +
							'<a href="###" class="icon module-close-btn"></a>' +
							'<div class="date-wrap">' +
								'<p class="date-wrap-single">' +
									'<input type="text" class="icon date-picker" value="2017-02-16" id="'+ self.domId + 'DatePicker" />' +
									'<a href="###" class="icon interval-btn" title="切换区间" id="'+ self.domId + 'IntervalBtn"></a>' +
								'</p>' +
								'<p class="date-wrap-interval hide">' +
									'<input type="text" class="icon date-picker" value="2017-02-16" id="'+ self.domId + 'SDatePicker" />' +
									'<i class="separator">-</i>' +
									'<input type="text" class="icon date-picker" value="2017-02-16" id="'+ self.domId + 'EDatePicker"  />' +
									'<a href="###" class="icon single-btn" title="切换区间" id="'+ self.domId + 'SingleBtn"></a>' +
								'</p>' +
							'</div>' +
							'<p class="module-name">'+ self.MODULENAME +'</p><i class="icon module-tip-btn" id="'+ self.domId +'ExplainBtn"><b class="icon"></b></i>' +
						'</div>' +
						'<div class="module-main">' +
							'<div class="chart-box" id="'+ self.domId +'ChartBox">' +
							'</div>' +
							'<div class="explain-box hide" id="'+ self.domId +'ExplainBox">' +
								'<p class="explain-text">'+ self.EXPLAINTEXT +'</p>' +
								'<div class="clearfix">' +
								'<div class="explain-left '+ self.domId +'-explain-pic">' +
								'</div>' +
								'<div class="explain-right">' +
									'<p>今日解读：<span id="'+ self.domId +'ExplainData">放量净流入12亿</span></p>' +
									'<br/>' +
									'<p>上涨概率回测</p>' +
									'<p id="'+ self.hcType +'HcData">历史数据回测中...</p>' +
								'</div>' +
								'</div>' +
							'</div>' +
							'<p class="non-trade-tip" id="'+ self.domId +'NonTradeTip">非交易日，请重新选择</p>' +
						'</div>' +
					'</div>' +
				'</li>';
				$('#modules-list-ul').append(htmlDom);
				self.renderData();

				self.bindEvents();
	}
	ZqxyModule.prototype.renderData = async function () {
		var tradeDay = await clientApi.getTradingDay();
		if (this.type == 'min') {
			this.requestMinData(tradeDay);
		} else {
			this.requestDayData(tradeDay);
		}
	}
	ZqxyModule.prototype.requestMinData = function (date) {
		var self = this;
		this.requestMin(this.code, date, function (data) {
			self.analyticalData(data);
		});
		this.getExplainData(date);
	}
	ZqxyModule.prototype.requestDayData = function (sdate, edate) {
		var self = this;
		this.requestDay(this.code, sdate, edate, function (data) {
			self.analyticalData(data);
		});
	}
	ZqxyModule.prototype.getExplainData = async function (date) {
		var hour = await clientApi.serverHour();

		if (hour > 0945) {
			this.requestZxData(this.hcType, date);
		} else {
			this.renderRisingProb(this.hcType, '');
		}
	}
	ZqxyModule.prototype.bindEvents = async function () {
		var self = this;
		var tradeDay = await clientApi.getTradingDay();

		var minDate = '2016-03-28';

		this.datePicker(tradeDay, this.domId, function (date) {
			self.requestMinData(date);
		}, function (sdate, edate) {
			self.requestDayData(sdate, edate);
		}, minDate);

		var hoverTimer;
		$('#'+ self.domId + 'ExplainBtn').hover(function () {
			var _this = this;
			hoverTimer = setTimeout(function () {
				$(_this).find('b').show();
            	$(_this).parents('.module-item').find('.explain-box').show();
			}, 1000);
		}, function () {
			clearTimeout(hoverTimer);
		});

		$('#'+ self.domId +'ExplainBox').mouseleave(function () {
			$(this).parents('.module-item').find('b.icon').hide();
			$(this).hide();
		});
	}
	ZqxyModule.prototype.analyticalData = async function (data, isUpdate) {
		var self = this;
		var mainDataLen = data[self.indexCode].length;
		var shDataLen = data['1A0001'].length;

		if (mainDataLen > 0) {
			self.latestVal1 = data[self.indexCode][mainDataLen - 1];
			self.latestVal2 = data['1A0001'][shDataLen - 1];
		} else {
			self.latestVal1 = '--';
			self.latestVal2 = '--';
		}
		if (mainDataLen > 0 && self.type == 'min') {
			$('#'+ self.domId +'ExplainBtn').show();
		} else {
			$('#'+ self.domId +'ExplainBtn').hide();
		}
		await self.drawChart(data, isUpdate);
		self.renderExplain(self.latestVal1);
	}

	ZqxyModule.prototype.renderExplain = function (data) {
		var self = this;
		if (isNaN(data)) {
			$('#'+ self.domId +'ExplainData').text(self.MODULENAME + '表现--');
			return;
		}
		var gradeTxt = '--',
			gradePic = '10';
		if (data > 4) {
			gradeTxt = '好';
		} else if (data > 2.5 && data <= 4) {
			gradeTxt = '较好';
		} else if (data > 1.5 && data <= 2.5) {
			gradeTxt = '一般';
		} else if (data > 0.5 && data <= 1.5) {
			gradeTxt = '较差';
		} else if (data <= 0.5) {
			gradeTxt = '差';
		}

		if (data <= -0.5) {
			gradePic = 0;
		} else if (data <= 0.5) {
			gradePic = 1;
		} else if (data <= 1) {
			gradePic = 2;
		} else if (data <= 1.5) {
			gradePic = 3;
		} else if (data <= 2) {
			gradePic = 4;
		} else if (data <= 2.5) {
			gradePic = 5;
		} else if (data <= 3) {
			gradePic = 6;
		} else if (data <= 4) {
			gradePic = 7;
		} else if (data <= 6) {
			gradePic = 8;
		} else if (data <= 10) {
			gradePic = 9;
		}
		$('#'+ self.domId +'ExplainData').text(self.MODULENAME + '表现' + gradeTxt);
		$('.'+ self.domId +'-explain-pic').css('background-position', -177 * gradePic +'px 0');
	}

	ZqxyModule.prototype.drawChart = function (data, isUpdate) {
		var self = this;
		var newestVal1 = isNaN(self.latestVal1) ? '--' : self.latestVal1.toFixed(2);
		var newestVal2 = isNaN(self.latestVal2) ? '--' : self.latestVal2.toFixed(2);
		var params = {
			indexName: '昨日涨停表现',
			series1: data[self.indexCode],
			series2: data['1A0001'],
			latestVal1: newestVal1,
			latestVal2: newestVal2,
			dateArr: data['dateArr']
		};
		if (isUpdate && self.HighchartsObj) {
			self.HighchartsObj.series[0].update({
				data: data[self.indexCode]
			});
			self.HighchartsObj.series[1].update({
				data: data['1A0001']
			});
			$('.' + self.domId + 'LegendData').text(newestVal1);
			$('.shLegendData').text(newestVal2);
		} else {
			this.renderChart(params);
		}
	}

	ZqxyModule.prototype.updateSeries = function (isHasOverall, date, result) {
		var self = this;
		if (isHasOverall) {
			return;
		}
		var inputDate = $('#'+ self.domId + 'DatePicker').val();
		var isToday = inputDate.replace(/-/g, '') == date;

		if (this.type == 'min' && isToday) {
			if (result) {
				self.analyticalData(result, 'isUpdate');
				// self.getExplainData(date);
			} else {
				self.requestMin(self.code, date, function (data) {
					self.analyticalData(data, 'isUpdate');
				});
				// self.getExplainData(date);
			}

		}
	}

	module.exports = ZqxyModule;
});